package cn.shop.dys.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import cn.shop.dys.R;
import cn.shop.dys.constant.Constant;
import cn.shop.dys.constant.SPConstant;
import cn.shop.dys.util.HttpUtils;
import cn.shop.dys.util.SPUtil;

public class SplashActivity extends AppCompatActivity {
    public static final String TAG = SplashActivity.class.getSimpleName();

    private ImageView mSplashItem_iv = null;
    private boolean isNotFirstin;
    private ImageView splash_bg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        splash_bg = (ImageView) findViewById(R.id.splash_bg);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        Constant.SCREEN_DENSITY = metrics.density;
        Constant.SCREEN_HEIGHT = metrics.heightPixels;
        Constant.SCREEN_WIDTH = metrics.widthPixels;
        isNotFirstin = SPUtil.getBoolean(this, SPConstant.ISNOTFIRSTIN);
        getNetWorkStata();
        findViewById();
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        splash_bg.setImageResource(R.mipmap.splash_bg);
    }

    @Override
    protected void onStop() {
        super.onStop();
        splash_bg.setImageBitmap(null);
    }

    protected void findViewById() {


        mSplashItem_iv = (ImageView) findViewById(R.id.splash_loading_item);
    }

    protected void initView() {

        Animation translate = AnimationUtils.loadAnimation(this,
                R.anim.splash_loading);
        translate.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {


            }

            @Override
            public void onAnimationRepeat(Animation animation) {


            }

            @Override
            public void onAnimationEnd(Animation animation) {

//                启动homeactivty，相当于Intent
                Class clazz = null;
                if (isNotFirstin) {
                    clazz = MainActivity2.class;
                } else {
                    clazz = GuideActivity.class;
                }
                openActivity(clazz);
                overridePendingTransition(R.anim.push_left_in,
                        R.anim.push_left_out);
                SplashActivity.this.finish();
            }
        });
        mSplashItem_iv.setAnimation(translate);
        translate.start();
    }

    private void openActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    public void getNetWorkStata() {
        boolean connectedInternet = HttpUtils.isConnectedInternet(this);
        SPUtil.put(this, SPConstant.ISCONNECTEDINTERNET, connectedInternet);
    }
}
